package com.tencent.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author: Fame
 * @date: 2022-06-20 14:40
 * @description:
 */
public class JDBCUtils {
    public static Connection getConnection(){
        Properties properties = new Properties();
//        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("E:\\qianfeng\\two\\guidu_servlet\\src\\main\\resources\\jdbc.properties");
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            properties.load(is);
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            Connection connection = dataSource.getConnection();
            return connection;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    public static void close(Connection conn, Statement ps, ResultSet rs){
        try {
            if (conn != null)
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (ps != null)
                ps.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            if (rs != null)
                rs.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
